package br.com.spread.svnutils;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.tmatesoft.svn.core.ISVNLogEntryHandler;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

public class History {
	static List<String> dirs = new ArrayList<String>();
	static List<String> aFiles = new ArrayList<String>();
	static List<String> mFiles = new ArrayList<String>();
	static List<String> rFiles = new ArrayList<String>();
	static List<String> releaseNotes = new ArrayList<String>();

	public static void main(String[] args) {
		DAVRepositoryFactory.setup();

		String url = "http://10.55.1.7:8080/svn/siaad";
		String name = "jose.nascimento";
		String password = "c4ss1n1";
		long startRevision = 3000;// Long.valueOf(args[3]);
		long endRevision = 1; // HEAD (the latest) revision

		SVNRepository repository = null;

		List<String> f = new ArrayList<String>();

		try {
			repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
			ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
			repository.setAuthenticationManager(authManager);

			final Collection<SVNLogEntry> logEntries = new LinkedList<SVNLogEntry>();
			repository.log(new String[] { "" }, startRevision, endRevision, true, true, 100, new ISVNLogEntryHandler() {
				public void handleLogEntry(SVNLogEntry logEntry) {
					logEntries.add(logEntry);
				}
			});

			for (Iterator<?> entries = logEntries.iterator(); entries.hasNext();) {
				SVNLogEntry logEntry = (SVNLogEntry) entries.next();

				releaseNotes.add("---------------------------------------------");
				releaseNotes.add("revision: " + logEntry.getRevision());
				releaseNotes.add("author: " + logEntry.getAuthor());

				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");
				releaseNotes.add("date: " + sdf.format(logEntry.getDate()));
				releaseNotes.add("log message: " + logEntry.getMessage());
				releaseNotes.add("");

				if (logEntry.getChangedPaths().size() > 0) {
					Set<?> changedPathsSet = logEntry.getChangedPaths().keySet();

					// System.out.println("Revision: " +
					// logEntry.getRevision());
					// System.out.println("Message: " + logEntry.getMessage());

					for (Iterator<?> changedPaths = changedPathsSet.iterator(); changedPaths.hasNext();) {
						SVNLogEntryPath entryPath = (SVNLogEntryPath) logEntry.getChangedPaths().get(
								changedPaths.next());

						// System.out.print(entryPath.getType());
						if (!f.contains(entryPath.getPath())) {
							f.add(entryPath.getPath());
						}

						releaseNotes.add(" "
								+ entryPath.getType()
								+ " "
								+ entryPath.getPath()
								+ ((entryPath.getCopyPath() != null) ? " (from " + entryPath.getCopyPath()
										+ " revision " + entryPath.getCopyRevision() + ")" : ""));

						if ("file".equals(String.valueOf(entryPath.getKind()))) {
							if ('A' == entryPath.getType() && !aFiles.contains(entryPath.getPath())) {
								aFiles.add(entryPath.getPath());
							}
							if ('M' == entryPath.getType() && !mFiles.contains(entryPath.getPath())) {
								mFiles.add(entryPath.getPath());
							}
							if ('R' == entryPath.getType() && !rFiles.contains(entryPath.getPath())) {
								rFiles.add(entryPath.getPath());
							}
						}
						if ("dir".equals(String.valueOf(entryPath.getKind())) && !dirs.contains(entryPath.getPath())) {
							dirs.add(entryPath.getPath());
						}
					}
				}
			}

			Collections.sort(f);

			for (String s : f) {
				System.out.println(s);
			}

		} catch (SVNException e) {
			e.printStackTrace();
		}

		try {
			writeReleaseNotes();
			writeArticactsList();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void writeReleaseNotes() throws Exception {
		FileWriter fw = new FileWriter(new File("release_notes.txt"));
		for (String line : releaseNotes) {
			fw.write(line + "\n");
		}
		fw.flush();
		fw.close();
	}

	private static void writeArticactsList() throws Exception {
		FileWriter fw = new FileWriter(new File("lista_artefatos.txt"));

		if (!dirs.isEmpty()) {
			fw.write("# Diretório(s):\n");
			Collections.sort(dirs);
			for (String dir : dirs) {
				fw.write(dir + "\n");
			}
		}

		if (!aFiles.isEmpty()) {
			fw.write("\n");
			fw.write("# Arquivo(s) novo(s):\n");
			Collections.sort(aFiles);
			for (String file : aFiles) {
				fw.write(file + "\n");
			}
		}
		if (!mFiles.isEmpty()) {
			fw.write("\n");
			fw.write("# Arquivo(s) modificado(s):\n");
			Collections.sort(mFiles);
			for (String file : mFiles) {
				fw.write(file + "\n");
			}
		}

		if (!rFiles.isEmpty()) {
			fw.write("\n");
			fw.write("# Arquivo(s) substituido(s):\n");
			Collections.sort(rFiles);
			for (String file : rFiles) {
				fw.write(file + "\n");
			}
		}

		fw.flush();
		fw.close();
	}
}
